define(['require' , 'exports' , 'module' , 'jquery' , 'layer' , 'common/common' , 'baiduTemplate' ] , function (require , exports , module , $ , layer , common) {

    var alertAddressHtml = function () {
        $('.cart-address').bind('click' , function () {
            window.location.href = "/user/address";
        });
    };

    var alertComfirmHtml = function () {
        $('.comfirm').bind('click' , function () {

            var addressId  = $('#choice-address').attr('address-id');

            common.ajaxPost('/order/init' , {
                address_id : addressId
            } , function (error , data) {
                if(error == null && data.code == 0){
                    window.location.href = '/order/comfirm/' + data.data;
                }else if(error == null) {
                    layer.open({
                        content : data.msg ,
                        skin : 'msg' ,
                        time : 2
                    });
                }
                else{
                    layer.open({
                        content : '结算失败,稍后再试' ,
                        skin : 'msg' ,
                        time : 2
                    });
                }

            });
        });
    };

    var pay = function() {
        function onBridgeReady() {
            var layerloading = layer.open({type: 2});
            var oid = $('#pay').attr('oid');

            common.ajaxPost('/wechat/pay' , {
                oid : oid
            } , function (error , data) {
                if(error == null && data.code == 0){
                    WeixinJSBridge.invoke(
                        'getBrandWCPayRequest', data.data,
                        function (res) {
                            if (res.err_msg == "get_brand_wcpay_request:ok") {
                                window.location.href = '/order/info/' + oid;
                            }
                        }
                    );
                }else if(error == null) {
                    layer.open({
                        content : data.msg ,
                        skin : 'msg' ,
                        time : 2
                    });
                }
                else{
                    layer.open({
                        content : '失败' ,
                        skin : 'msg' ,
                        time : 2
                    });
                }

                layer.close(layerloading);

            });
        }

        $('#pay').bind('click' , function () {
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
        });

    };

    exports.pay = pay;

    exports.alertComfirmHtml = alertComfirmHtml;
    exports.alertAddressHtml = alertAddressHtml;
});